package me.zhengjie.utils;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.lang.management.ManagementFactory;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 时间工具类
 *
 * @author ruoyi
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static String HH_MM_SS = "HH:mm:ss";
    public static String HH_MM = "HH:mm";

    private static String[] parsePatterns = {
            "yyyy-MM-dd" , "yyyy-MM-dd HH:mm:ss" , "yyyy-MM-dd HH:mm" , "yyyy-MM" ,
            "yyyy/MM/dd" , "yyyy/MM/dd HH:mm:ss" , "yyyy/MM/dd HH:mm" , "yyyy/MM" ,
            "yyyy.MM.dd" , "yyyy.MM.dd HH:mm:ss" , "yyyy.MM.dd HH:mm" , "yyyy.MM"};

    /**
     * 获取当前Date型日期
     */
    public static Timestamp getNowTimestamp() {
        return new Timestamp(System.currentTimeMillis());
    }

    //获取今天起始时间
    public static Timestamp getDayStartTimestamp() {
        return new Timestamp(getDayStartTime().getTime());
    }

    //获取今天结束时间
    public static Timestamp getDayEndTimestamp() {
        return new Timestamp(getDayEndTime().getTime());
    }

    //获取今天结束时间+long
    public static Timestamp getDayEndPlusTimestamp(long add) {
        return new Timestamp(getDayEndTime().getTime()+add);
    }

    //获取昨天起始时间
    public static Timestamp getYesterDayStartTimestamp() {
        return new Timestamp(getYesterDayStartTime().getTime());
    }

    //获取昨天结束时间
    public static Timestamp getYesterDayEndTimestamp() {
        return new Timestamp(getYesterDayEndTime().getTime());
    }

    //Timestamp 转 字符串
    public static String TimestampToStrDay(Timestamp timestamp){
        Date date = new Date(timestamp.getTime());
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static String TimestampToStrHM(Timestamp timestamp){
        Date date = new Date(timestamp.getTime());
        return parseDateToStr(HH_MM, date);
    }



    public static Date getNowDate() {
        return new Date();
    }

    /**
     * 获取今天开始时间
     */
    private static Date getDayStartTime() {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
//        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime();
    }

    public static long getDayStartTimelong() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }


    /**
     * 获取指定某一天的开始时间戳
     *
     * @param timeStamp 毫秒级时间戳
     * @param timeZone  如 GMT+8:00
     * @return
     */
    public static Long getDailyStartTime(Long timeStamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timeStamp);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }
    /**
     * 获取指定某一天的结束时间戳
     *
     * @param timeStamp 毫秒级时间戳
     * @param timeZone  如 GMT+8:00
     * @return
     */
    public static Long getDailyEndTime(Long timeStamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timeStamp);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
//        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTimeInMillis();
    }

    public static Timestamp longToTimeStamp(Long time){
        return new Timestamp(time);
    }

    /**
     * 获取今天结束时间
     */
    private static Date getDayEndTime() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR, 11);    //这里不能用23，奇怪
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 0);
        return todayEnd.getTime();
    }


    /**
     * 获取昨天开始时间
     */
    private static Date getYesterDayStartTime() {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
//        todayStart.set(Calendar.MILLISECOND, 0);
        todayStart.add(Calendar.DAY_OF_MONTH,-1);
        return todayStart.getTime();
    }

    /**
     * 获取今天结束时间
     */
    private static Date getYesterDayEndTime() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
//        todayEnd.set(Calendar.MILLISECOND, 999);
        todayEnd.add(Calendar.DAY_OF_MONTH,-1);
        return todayEnd.getTime();
    }
    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate() {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime() {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String getTime3() {
        return dateTimeNow(HH_MM);
    }

    public static final String getTime3(Long time) {
        return dateTimeNow(HH_MM,time);
    }

    public static final String dateTimeNow() {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format) {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTimeNow(final String format,Long time) {
        return parseDateToStr(format, new Date(time));
    }

    public static final String dateTime(final Date date) {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date) {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date strToDate(final String format, final String ts) {
        try {
            return new SimpleDateFormat(format).parse(ts);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static final Date strToDate(String ts) {
        try {
            return new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).parse(ts);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static final Long strToLong(String ts) {
        try {
            return new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).parse(ts).getTime();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static final Date dateTimeToDay( final String ts) {
        try {
            return new SimpleDateFormat("yyyy-MM-dd").parse(ts);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }
    public static final String formatDate(Date date,String pattern) {
        return DateFormatUtils.format(date, pattern);
    }
    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

    public static Timestamp parseTimestamp(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return new Timestamp(parseDate(str.toString(), parsePatterns).getTime());
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * monthStr 格式如：2020-08
     * @param monthStr
     * @return
     */
    public static List<Timestamp> getMonthBetween(String monthStr){

        monthStr += "-01";
        Date mDate = parseDate(monthStr);
        Calendar cale = Calendar.getInstance();
        cale.setTime(mDate);

        // 获取当月第一天和最后一天
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String firstday, lastday;

        // 获取前月的第一天
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        firstday = format.format(cale.getTime());

        // 获取前月的最后一天
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        lastday = format.format(cale.getTime());

        List<Timestamp> ret = new ArrayList<>();
        ret.add(parseTimestamp(firstday));
        ret.add(parseTimestamp(lastday));
        return  ret;
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    /**
     * 计算请假天数
     */
    public static float getHolidayPoor(Timestamp endDate, Timestamp nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
//        long hour = diff % nd / nh;

        float ret = day;
//        if(hour>=4){
//            ret+=0.5;
//        }
//        return day + "天" + hour + "小时";
        return ret+1;
    }

    public static String getHello() {

        String ret = "";
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("HH");
        String str = df.format(date);
        int a = Integer.parseInt(str);
        if (a >= 0 && a <= 6) {
            ret = "凌晨好";
        }
        if (a > 6 && a <= 12) {
            ret = "上午好";
        }
        if (a > 12 && a <= 13) {
            ret = "中午好";
        }
        if (a > 13 && a <= 18) {
            ret = "下午好";
        }
        if (a > 18 && a <= 24) {
            ret = "晚上好";
        }
        return ret;
    }

    //从时间字符串或当前时间，取年，月，日，时，分，秒
    public static int getTimeSplit(int flag,String timeStr){

        Calendar calendar = Calendar.getInstance();
        Date date;
        if(!timeStr.equals("")){
            date = DateUtils.parseDate(timeStr);
            calendar.setTime(date);
        }else{
            calendar.setTime(new Date());
        }

        int i = 0;
        switch (flag){
            case 1:
                i = calendar.get(Calendar.YEAR); break;//获取年份
            case 2:
                i = calendar.get(Calendar.MONTH); break;//获取月份
            case 3:
                i = calendar.get(Calendar.DATE); break;//获取本月第几日
            case 4:
                i = calendar.get(Calendar.HOUR_OF_DAY); break;//获取24小时制时
            case 5:
                i = calendar.get(Calendar.MINUTE); break;//获取分
            case 6:
                i = calendar.get(Calendar.SECOND); break;//获取秒
        }
        return i;
    }

    //从当前时间，取时，分，秒+总秒数
    public static int[] getTimeSplitAll(){

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());

        int h = calendar.get(Calendar.HOUR_OF_DAY);
        int m = calendar.get(Calendar.MINUTE);
        int s = calendar.get(Calendar.SECOND);
        int all = h*3600+m*60+s;
        return new int[]{h,m,s,all};
    }


 }
